﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Antu.Business;
using Antu.Business.Business;
using Antu.Business.AbstractClass;
using System.Xml;

namespace DataTranslation
{
    public partial class FrmMain : Form
    {
        private AbstractDataOperation clsDataTranlation;

        public FrmMain()
        {
            InitializeComponent();
        }

        #region 外网导出为临时文件
        private void btnExport_Click(object sender, EventArgs e)
        {
            LockControl(false);
            try
            {
                for (int i = 1; i < 11; i++)
                {
                    CheckBox chk = this.Controls["groupBox1"].Controls.Find("checkBox" + i.ToString(), false)[0] as CheckBox;
                    if (chk.Checked)
                    {
                        DataSet ds = new DataSet();
                        chk.ForeColor = Color.Red;

                        GlobalSetting.SystemTranslationType = GetOperationType(i);　　　//设置用户倒换的数据类型

                        clsDataTranlation = DataTranslationFactory.CreateDataOperation( GlobalSetting.SystemTranslationType);

                        clsDataTranlation.progress = this.progressBar1;
                        clsDataTranlation.lblMessage = this.lblMessage;
                        DataSet dsGet = clsDataTranlation.WebToTempFile(DataTranslationFactory.ExportTables);  //提取数据操作
                        for (int k = 0; k < dsGet.Tables.Count; k++)
                        {
                            DataTable dtTemp = dsGet.Tables[k].Copy();
                            ds.Tables.Add(dtTemp);
                        }
                        if (!Directory.Exists("WorkDir"))
                        {
                            Directory.CreateDirectory("WorkDir");
                        }

                        string strFileName = "WorkDir\\" + chk.Text + "_web.xml";

                        //转变编码
                        XmlTextWriter xtwTemp = new XmlTextWriter(strFileName, Encoding.Unicode);
                        ds.WriteXml(xtwTemp);
                        xtwTemp.Close();

                        chk.ForeColor = Color.Green;
                        ds.Dispose();
                    }
                }
                lblMessage.Text = "导出成功";
                MessageBox.Show("导出成功");
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                //移动到结果文件夹中
                string strResultDir = "Result_web_" + DateTime.Now.ToString().Replace(":", "：");
                Directory.CreateDirectory(strResultDir);
                foreach (string strFile in Directory.GetFiles("WorkDir"))
                {
                    File.Move(strFile, strResultDir + strFile.Replace("WorkDir", ""));
                }
                LockControl(true);
            }
        }
        #endregion

        #region 临时文件导入内网
        private void btnImport_Click(object sender, EventArgs e)
        {
            LockControl(false);
            try
            {
                for (int i = 1; i < 11; i++)
                {
                    CheckBox chk = this.Controls["groupBox1"].Controls.Find("checkBox" + i.ToString(), false)[0] as CheckBox;
                    if (chk.Checked)
                    {
                        //获取选择表的数据
                        DataSet dsGet = new DataSet();
                        string strFileName="WorkDir\\" + chk.Text + "_web.xml";
                        if (!File.Exists(strFileName))
                        {
                            ClsMsgBoxAppear.MsgBoxError("\"" + chk.Text + "\"源文件不存在！");
                            continue;
                        }
                        lblMessage.Text = "数据读取中...请等待";

                        Application.DoEvents();
                        dsGet.ReadXml(strFileName);
                        if (dsGet.Tables.Count == 0)
                        {
                            ClsMsgBoxAppear.MsgBoxError("\"" + chk.Text + "\"源文件为空！");
                            File.Delete(strFileName); //删除文件
                            continue;
                        }
                        chk.ForeColor = Color.Red;
                        GlobalSetting.SystemTranslationType = GetOperationType(i);
                        clsDataTranlation = DataTranslationFactory.CreateDataOperation(GlobalSetting.SystemTranslationType);
                        clsDataTranlation.progress = this.progressBar1;
                        clsDataTranlation.lblMessage = this.lblMessage;
                        clsDataTranlation.TempFileToInside(DataTranslationFactory.ExportTables, dsGet);
                        chk.ForeColor = Color.Green;

                        File.Delete(strFileName);//删除文件
                    }
                }
                lblMessage.Text = "导入成功";
                MessageBox.Show("导入成功");
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                LockControl(true);
            }
        }
        #endregion

        #region 内网导出为临时文件
        private void btnExportFromInside_Click(object sender, EventArgs e)
        {
            LockControl(false);
            try
            {
                for (int i = 0; i < 12; i++)
                {
                    CheckBox chk = this.Controls["groupBox1"].Controls.Find("checkBox" + i.ToString(), false)[0] as CheckBox;
                    if (chk.Checked)
                    {
                        DataSet ds = new DataSet();
                        chk.ForeColor = Color.Red;
                        GlobalSetting.SystemTranslationType = GetOperationType(i);
                        clsDataTranlation = DataTranslationFactory.CreateDataOperation(
                            GlobalSetting.SystemTranslationType);
                        clsDataTranlation.progress = this.progressBar1;
                        clsDataTranlation.lblMessage = this.lblMessage;
                        DataSet dsGet = clsDataTranlation.InsideToTempFile(DataTranslationFactory.ExportTables);
                        for (int k = 0; k < dsGet.Tables.Count; k++)
                        {
                            DataTable dtTemp = dsGet.Tables[k].Copy();
                            ds.Tables.Add(dtTemp);
                        }
                        if (!Directory.Exists("WorkDir"))
                        {
                            Directory.CreateDirectory("WorkDir");
                        }

                        //转变编码
                        string strFileName = "WorkDir\\" + chk.Text + "_inside.xml";
                        XmlTextWriter xtwTemp = new XmlTextWriter(strFileName, Encoding.Unicode);
                        ds.WriteXml(xtwTemp);
                        xtwTemp.Close();

                        chk.ForeColor = Color.Green;
                        ds.Dispose();
                    }
                }

                lblMessage.Text = "导出成功";
                MessageBox.Show("导出成功");
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                //移动到结果文件夹中
                string strResultDir = "Result_Inside_" + DateTime.Now.ToString().Replace(":", "：");
                Directory.CreateDirectory(strResultDir);
                foreach (string strFile in Directory.GetFiles("WorkDir"))
                {
                    File.Move(strFile, strResultDir + strFile.Replace("WorkDir", ""));
                }
                LockControl(true);
            }
        }
        #endregion

        #region 临时文件导入外网
        private void btnImportFromWeb_Click(object sender, EventArgs e)
        {
            LockControl(false);
            try
            {
                for (int i = 0; i < 12; i++)
                {
                    CheckBox chk = this.Controls["groupBox1"].Controls.Find("checkBox" + i.ToString(), false)[0] as CheckBox;
                    if (chk.Checked)
                    {
                        //获取选择表的数据
                        DataSet dsGet = new DataSet();
                        string strFileName = "WorkDir\\" + chk.Text + "_inside.xml";
                        if (!File.Exists(strFileName))
                        {
                            ClsMsgBoxAppear.MsgBoxError("\"" + chk.Text + "\"源文件不存在！");
                            continue;
                        }

                        lblMessage.Text = "数据读取中...请等待";

                        Application.DoEvents();
                        dsGet.ReadXml(strFileName);
                        if (dsGet.Tables.Count == 0)
                        {
                            ClsMsgBoxAppear.MsgBoxError("\"" + chk.Text + "\"源文件为空！");
                            File.Delete(strFileName); //删除文件
                            continue;
                        }
                        chk.ForeColor = Color.Red;
                        GlobalSetting.SystemTranslationType = GetOperationType(i);
                        clsDataTranlation = DataTranslationFactory.CreateDataOperation(
                            GlobalSetting.SystemTranslationType);
                        clsDataTranlation.progress = this.progressBar1;
                        clsDataTranlation.lblMessage = this.lblMessage;
                        clsDataTranlation.TempFileToWeb(DataTranslationFactory.ExportTables, dsGet);
                        chk.ForeColor = Color.Green;

                        File.Delete(strFileName); //删除文件
                    }
                }
                lblMessage.Text = "导入成功";
                MessageBox.Show("导入成功");
            }
            catch (Exception ex)
            {
                ClsMsgBoxAppear.MsgBoxError(ex);
            }
            finally
            {
                LockControl(true);
            }
        }
        #endregion

        private void CheckAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++)
            {
                CheckBox chk = this.Controls["groupBox1"].Controls.Find("checkBox" + i.ToString(), false)[0] as CheckBox;
                chk.Checked = CheckAll.Checked;
            }
        }

        /// <summary>
        /// 确定要操作的系统类型
        /// </summary>
        /// <param name="checkNum">选中的checkBox编号</param>
        /// <returns>操作的系统类型</returns>
        private OperationType GetOperationType(int checkNum)
        {
            switch (checkNum)
            {
                case 0:         //用户签名数据倒换
                    return OperationType.UserSignExchange;
                case 1:
                    return OperationType.WuYeRegisterExchange;
                case 2:
                    return OperationType.WuYeRequestExchange;
                case 3:
                    return OperationType.ChaiQianSysExchange;
                case 4:
                    return OperationType.ZlQueryExchange;
                case 5:
                    return OperationType.WyRepairExchange;
                case 6:
                    return OperationType.JjRequestExchange;
                case 7:
                    return OperationType.PgRequestExchange;
                case 8:
                    return OperationType.ZfRequestExchange;
                case 9:
                    return OperationType.QxTaskDateExchange;
                case 10:
                    return OperationType.HouseStructExchange;
                case 11:
                    return OperationType.TaskDataExchange;
                default:
                    return OperationType.NullExchange;
            }
        }

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        /// <param name="status">状态</param>
        private void LockControl(bool status)
        {
            this.btnExport.Enabled = status;
            this.btnImport.Enabled = status;
            this.btnExportFromInside.Enabled = status;
            this.btnImportFromWeb.Enabled = status;
        }

        /// <summary>
        /// 修改文件的编码
        /// </summary>
        /// <param name="strSourceFile"></param>
        /// <param name="strTargetFile"></param>
        private void ChangeFileCoding(string strSourceFile,string strTargetFile)
        {
            StreamReader srTemp = new StreamReader(strSourceFile, Encoding.Default);
            StreamWriter swTemp = new StreamWriter(strTargetFile, false, Encoding.GetEncoding("GB2312"));
            swTemp.Write(srTemp.ReadToEnd());
            srTemp.Close();
            swTemp.Close();
        }
    }
}